<template>
  <div class="sidebar-container">
    <el-menu
      v-if="showElMenu"
      class="sidebar-menu"
      :default-active="activeIndex"
      :collapse="false"
      @select="handleSelect"
    >
      <el-menu-item v-for="item in items" :key="item.index" :index="item.index">
        <!-- 支持自定义图标 div 或 element-plus 图标 -->
        <template v-if="item.customIcon">
          <div
            class="icon-wrapper"
            :class="[
              item.iconClass,
              { [item.activeIconClass]: activeIndex === item.index },
            ]"
          ></div>
        </template>
        <template v-else>
          <el-icon><component :is="item.icon" /></el-icon>
        </template>
        <span>{{ item.title }}</span>
      </el-menu-item>
    </el-menu>
    <el-button 
      class="toggle-button" 
      @click="showElMenu = !showElMenu"
      :class="{ 'collapsed': !showElMenu }"
    >
      <el-icon><component :is="showElMenu ? 'DArrowLeft' : 'DArrowRight'" /></el-icon>
    </el-button>
  </div>
</template>

<script setup>
import { defineProps, defineEmits, ref } from "vue";

const emit = defineEmits(["select"]);
const showElMenu = ref(true);

const props = defineProps({
  items: {
    type: Array,
    required: true,
  },
  activeIndex: {
    type: String,
    required: true,
  },
});

const handleSelect = (index) => {
  emit("select", index);
};
</script>

<style scoped>
.sidebar-container {
  position: relative;
  display: flex;
  align-items: flex-start;
  height: 100%;
  min-height: inherit;
}

.sidebar-menu {
  width: 150px;
  position: sticky;
  top: 0;
  left: 0;
  transition: all 0.3s ease;
  height: 100%;
  min-height: inherit;
  display: flex;
  flex-direction: column;
}

.toggle-button {
  position: absolute;
  right: -32px;
  top: 10px;
  width: 32px;
  height: 32px;
  padding: 0;
  border-radius: 0 4px 4px 0;
  background-color: #f2f6fc;
  border: 1px solid #dcdfe6;
  border-left: none;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
  z-index: 1;
  box-shadow: 1px 0 3px rgba(0, 0, 0, 0.1);
  color: #409eff;
}

.toggle-button:hover {
  background-color: #ecf5ff;
  color: #66b1ff;
}

.toggle-button.collapsed {
  right: 0;
  border-radius: 4px;
  border-left: 1px solid #dcdfe6;
  box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.1);
}

.icon-wrapper {
  width: 20px;
  height: 20px;
  margin-right: 5px;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
}
</style>
